% clc;    % Clear the command window.
% close all;  % Close all figures (except those of imtool.)
% clear;  % Erase all existing variables. Or clearvars if you want.
% 
% x0=10;
% y0=10;
% width=600;
% height=850;
% 
% % Load data from Stata export
% data = readtable('cat2_attribution_groups.csv');
% 
% % Extract values
% cat2_share_up = data.cat2_share_up;
% 
% % Custom titles for each group
% custom_titles = {
%     'T1 (lower LTI)'
%     'T2 (higher LTI)'
%     'T3 (lower LTV-BTL)'
%     'T4 (higher LTV-BTL)'
%     'T5 (lower LTV-Own)'
%     'T6 (higher LTV-Own)'
% };
% 
% % Create 2×3 subplot layout
% figure;
% for i = 1:6
%     subplot(3, 2, i);
% 
%     % Plot single bar for price expectations' attribution
%     bar(cat2_share_up(i), 'FaceColor', [0.2 0.6 0.9]);
%     set(gca, 'XTickLabel', {''}, 'FontSize', 10);
% 
%     % Custom title for each group
%     title(custom_titles{i}, 'FontWeight', 'normal');
% 
%     % Y-axis formatting
% 
%     if i == 1 || i == 3 || i == 5
%         ylabel('% Attribution');
%     else
%         ylabel('');
%     end
% 
%     ylim([0 70]);
%     grid on;
% 
%     % Add value label above the bar
%     text(1, cat2_share_up(i) + 1, sprintf('%.1f%%', cat2_share_up(i)), ...
%          'HorizontalAlignment', 'center', 'FontSize', 10);
% end
% 
% % Add overall figure title
% %sgtitle('Attribution of Changing Home Buying Intention to Hosue Price Expectations', ...
% %        'FontWeight', 'bold', 'FontSize', 13);
% 
% dpi = 100;
% width_in = width / dpi;
% height_in = height / dpi;
% 
% set(gcf, 'PaperUnits', 'inches');
% set(gcf, 'PaperSize', [width_in, height_in]);
% set(gcf, 'PaperPosition', [0, 0, width_in, height_in]);
% set(gcf, 'PaperPositionMode', 'manual');
% saveas(gcf,"plot_share_cat2.pdf",'pdf')


clc;
close all;
clear;

% Load data
data = readtable('cat2_attribution_groups.csv');
cat2_share_up = data.cat2_share_up;

% Custom labels
custom_titles = {
    'T1 (lower LTI)'
    'T2 (higher LTI)'
    'T3 (lower LTV-BTL)'
    'T4 (higher LTV-BTL)'
    'T5 (lower LTV-Own)'
    'T6 (higher LTV-Own)'
};

% Create one grouped bar plot
figure('Units','inches','Position',[1, 1, 6, 4.5]);

bar(cat2_share_up, 'FaceColor', [0.2 0.6 0.9]);
set(gca, 'XTick', 1:6, 'XTickLabel', custom_titles, 'FontSize', 10);
ylabel('% Attribution');
ylim([0 70]);
grid on;

% Rotate x-axis labels if needed
xtickangle(30);

% Add value labels above bars
for i = 1:length(cat2_share_up)
    text(i, cat2_share_up(i) + 1, sprintf('%.1f%%', cat2_share_up(i)), ...
         'HorizontalAlignment', 'center', 'FontSize', 10);
end

% Title
% title('Attribution of Change in Home Buying Intention to House Price Expectations', ...
%      'FontWeight', 'bold', 'FontSize', 12);

% Export to PDF
dpi = 100;
width=800;
height=600;
width_in = width / dpi;
height_in = height / dpi;

set(gcf, 'PaperUnits', 'inches');
set(gcf, 'PaperSize', [width_in, height_in]);
set(gcf, 'PaperPosition', [0, 0, width_in, height_in]);
set(gcf, 'PaperPositionMode', 'manual');
saveas(gcf,"plot_share_cat2_all.pdf",'pdf')


% clc;    % Clear the command window.
% close all;  % Close all figures (except those of imtool.)
% clear;  % Erase all existing variables. Or clearvars if you want.
% 
% 
% % Load overall attribution from Stata
% data = readtable('cat2_attribution_overall.csv');
% 
% % Extract value
% cat2_share_up = data.cat2_share_up;
% 
% % Plot single bar
% figure;
% bar(cat2_share_up, 'FaceColor', [0.2 0.6 0.9]);
% set(gca, 'XTickLabel', {''}, 'FontSize', 11);
% ylabel('% Attribution');
% ylim([0 60]);
% grid on;
% 
% % Add label
% text(1, cat2_share_up + 1, sprintf('%.1f%%', cat2_share_up), ...
%      'HorizontalAlignment', 'center', 'FontSize', 11);
% 
% % Title
% title('Overall attribution of Changing Home Buying Intention to Hosue Price Expectations', ...
%       'FontWeight', 'bold', 'FontSize', 13);
% 
% dpi = 50;
% width_in = width / dpi;
% height_in = height / dpi;
% 
% set(gcf, 'PaperUnits', 'inches');
% set(gcf, 'PaperSize', [width_in, height_in]);
% set(gcf, 'PaperPosition', [0, 0, width_in, height_in]);
% set(gcf, 'PaperPositionMode', 'manual');
% saveas(gcf,"Attribution_cat2_overall.pdf",'pdf')
